package com.sumainfor.pc.market.service.gmio;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sumainfor.common.util.CustomMap;
import com.sumainfor.common.util.DateUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.market.dao.ShowHousingStateMapper;
import com.sumainfor.pc.market.feign.BaseFeignEpc;
import com.sumainfor.pc.market.feign.BaseFeignGmio;
import com.sumainfor.pc.market.feign.BaseFeignZuul;
import com.sumainfor.pc.market.feign.TenantFeign;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author Eddy·Chen
 * @Email 835033913@qq.com
 * @Create 2019-05-27 16:17
 */
@Service
@Log4j2
public class ShowHousingStateService {

    @Autowired
    private ShowHousingStateMapper mapper;

    @Autowired
    private BaseFeignEpc epc;

    @Autowired
    private TenantFeign tenantFeign;

    @Autowired
    BaseFeignGmio baseFeignGmio;

    @Autowired
    BaseFeignZuul baseFeignZuul;

    /**
     * 1.	承租数据报表
     *
     * @param page
     * @param params: MiId - 服务中心编号
     *                ComId - 项目编号
     *                search - 关键字查询
     *                startTime - 开始时间
     *                endTime - 结束时间
     * @return
     */
    public JsonResult lesseeHousingTable(Page page, Map<String, Object> params) {
        IPage<Map<String, Object>> dataPage = mapper.selectLesseeHousingTable(page, params);
        for (Map<String, Object> map : dataPage.getRecords()) {
            DateUtils.formatDateByMap(map);
        }
        return JsonResult.success(dataPage);
    }

    /**
     * 2.	装修中房源报表
     *
     * @param page
     * @param params: MiId - 服务中心编号
     *                ComId - 项目编号
     *                search - 关键字查询
     *                startTime - 开始时间
     *                endTime - 结束时间
     * @return
     */
    public JsonResult decorationHousingTable(Page page, Map<String, Object> params) {

        JsonResult<List<Map<String, Object>>> idResult = epc.selectPlanHousingTableForAll(params);
        if (!idResult.isSuccess()) {
            return idResult;
        }
        Pager pager = new Pager();
        pager.setPagesize(Integer.valueOf(page.getSize() + ""));
        pager.setPageno(Integer.valueOf(page.getCurrent() + ""));

        List<Map<String, Object>> list = idResult.getResult();
        List<Map<String, Object>> data = Lists.newArrayList();
        CustomMap selectWhere = CustomMap.create("ids", list.stream().map(item -> MapUtils.getString(item, "HouId")).collect(Collectors.toList()));
        selectWhere.putAll(params);
        List<Map<String, Object>> selectResult = mapper.getHousingInfoData(selectWhere);
        for (Map<String, Object> map : selectResult) {
            for (Map<String, Object> item : list) {
                if (Objects.equals(MapUtils.getString(item, "HouId"), MapUtils.getString(map, "HouId"))) {
                    item.putAll(map);
                    data.add(item);
                    break;
                }
            }
        }
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        JsonResult<Map<String, Object>> jsonResult = pageUtils.getJsonResult(data, params, list.size());

        page.setTotal(data.size());
        page.setPages(MapUtils.getLong(jsonResult.getResult(), "totalPage"));

        long start = (page.getCurrent() - 1) * page.getSize();
        long end = page.getCurrent() * page.getSize();
        if (end >= data.size()) {
            end = data.size();
        }

        List result = Lists.newArrayList();
        for (long i = start; i < end; i++) {
            result.add(data.get((int) i));
        }
        page.setRecords(result);
        return JsonResult.success(page);
    }


    /**
     * 3.	租赁房源报表
     *
     * @param page
     * @param params: MiId - 服务中心编号
     *                ComId - 项目编号
     *                search - 关键字查询
     *                startTime - 开始时间
     *                endTime - 结束时间
     * @return
     */
    public JsonResult selectLeaseIngHousingTable(Page page, Map<String, Object> params) {
        return JsonResult.success(mapper.selectLeaseIngHousingTable(page, params));
    }


    /**
     * 租客管理页面主表格数据
     *
     * @param page
     * @param params
     * @return
     */
    public JsonResult getTenantTableList(Page page, Map<String, Object> params) {
        //搜索内容
        String search = MapUtils.getString(params, "search");
        Page<Map<String, Object>> pageList = tenantFeign.selectTenantTableList(CustomMap.create("current", page.getCurrent())
                .put("size", page.getSize())
                .put("search", search));
        //查询签约数
        for (Map<String, Object> map : pageList.getRecords()) {
            String tenId = MapUtils.getString(map, "TenId");
            int pactCount = mapper.selectPactCountForTenantId(tenId);
            map.put("PactCount", pactCount);
        }
        return JsonResult.success(pageList);
    }


    /**
     * 查询这个租客的历史合同信息
     *
     * @param tenId
     * @return
     */
    public JsonResult getTenantPactInfo(String tenId) {
        List<Map<String,Object>> zczsList = mapper.selectTenantPactInfo(tenId);
        if (zczsList.size()>0){
            for (Map<String,Object> zczsMap:zczsList) {
                StringBuffer zczsString  = new StringBuffer();
                String HouIdString;
                zczsString.append(MapUtils.getString(zczsMap,"ComName")).append(MapUtils.getString(zczsMap,"BuilDing")).append("号楼").append(MapUtils.getString(zczsMap,"Room")).append("室").append(MapUtils.getString(zczsMap,"cod"));
                  HouIdString = zczsString.toString();
                  zczsMap.put("HouIdString",HouIdString);
                  if (zczsMap.get("PaId")!=null){
                      //添加合租人信息
                      zczsMap.put("pactShared",mapper.getPactShared(zczsMap));
                      //查询账单详情列表
                      zczsMap.put("pactBill",baseFeignGmio.getPaIdTenantBill(zczsMap));

                  }
                  if(zczsMap.get("SysUserId")!=null){
                      zczsMap.put("user_id",MapUtils.getString(zczsMap,"SysUserId"));
                      JsonResult zuulResult = baseFeignZuul.getSystemUser(zczsMap);
                      Map<String,Object> zuulMap = (Map<String, Object>) zuulResult.get("result");
                      if (zuulMap !=null){
                          zczsMap.putAll(zuulMap);
                      }
                  }
            }
        }



        return JsonResult.success(zczsList);
    }

}
